﻿using Cart.NguoiDungSrv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class LoginRegister : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEmailDN.Attributes.Add("onblur", "javascript:if(this.value == '')this.value='Email đăng nhập';");
            txtEmailDN.Attributes.Add("onfocus", "javascript:if(this.value == 'Email đăng nhập') this.value='';");
            txtPassDN.Attributes.Add("value", "!@#$%^");
            txtPassDN.Attributes.Add("onblur", "javascript:if(this.value == '')this.value='!@#$%^';");
            txtPassDN.Attributes.Add("onfocus", "javascript:if(this.value == '!@#$%^') this.value='';");
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            using (var nguoidungService = new NguoiDungSrv.NguoiDungServiceClient())
            {
                var check = nguoidungService.CheckEmail(txtEmail.Text);
                if (check == true)
                {
                    var nguoidungmodel = new Cart.NguoiDungSrv.NguoiDung();
                    nguoidungmodel.Email = txtEmail.Text;
                    nguoidungmodel.MatKhau = txtPass3.Text;
                    nguoidungmodel.DienThoai = txtDienThoai.Text;
                    nguoidungmodel.DiaChi = txtDiaChi.Text;
                    nguoidungmodel.HoTen = txtHoTen.Text;
                    var result = nguoidungService.CapNhatNguoiDung(nguoidungmodel);
                    //Response.Redirect("~/MuaHang.aspx");
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Đăng ký" + result + "');", true);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Đăng ký " + result + "'); window.location='" + Request.ApplicationPath + "MuaHang.aspx';", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Email đã được sử dụng');", true);
                }
            }
            //Response.Redirect("~/MuaHang.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (var ndService = new NguoiDungServiceClient())
            {
                var result = ndService.Login(txtEmail.Text, txtPassDN.Text);
                if (result != null)
                {
                    if (result.TrangThai == 1)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Tài khoản đã bị khóa !'); window.location='" + Request.ApplicationPath + "MuaHang.aspx';", true);
                    }
                    else
                    {
                        Session["nsd"] = result;
                        Response.Redirect("~/MuaHang.aspx");
                    }

                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Tài khoản hoặc mật khẩu sai !'); window.location='" + Request.ApplicationPath + "MuaHang.aspx';", true);
                }
            }



        }
    }
}